/** Concrete implementation for IPropertyService
 *
 *  @author Scott Monahan
 *  @version $Id$
 *  
 *  File last changed on $Date:$ by $Author:$ in $Rev:$
 *
 */

package info637.pprc.services;

import java.util.ArrayList;
import java.util.List;

import info637.pprc.ErrorCodes;
import info637.pprc.model.Property;
import info637.pprc.model.Session;
import info637.pprc.repository.IPropertyRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value = "propertyService")
public class PropertyService implements IPropertyService {

	@Autowired
	private IPropertyRepository propertyRepository = null;
	
	@Autowired 
	private ISessionService sessionService = null;

	@SuppressWarnings("serial")
	@Override
	public LoginResponse login(String userName, String password) {
		
		// TODO: Replace with use of login service in later cycle
		
		if (userName.equalsIgnoreCase("adminUser1")) {
				
			if (password.equalsIgnoreCase("adminPassword1")) {
				
				Session session = sessionService.createSession();
				
				return new LoginResponse(session.getSessionId());
				
			} else {
					
				return new LoginResponse(
					new ArrayList<ErrorCodes>() {{ add(ErrorCodes.INVALID_USER_NAME_OR_PASSWORD); }}
				);
			}
			
		} else {
			
			return new LoginResponse(
				new ArrayList<ErrorCodes>() {{ add(ErrorCodes.INVALID_USER_NAME_OR_PASSWORD); }}
			);
		}
	}

	@Override
	public AddPropertyResponse addProperty(Property propertyToAdd) {
		
		if (propertyRepository.addProperty(propertyToAdd)) {
		
			return new AddPropertyResponse();
			
		} else {
			
			List<ErrorCodes> errors = new ArrayList<ErrorCodes>();
			
			errors.add(ErrorCodes.PROPERTY_ALREADY_EXISTS);
			
			return new AddPropertyResponse(errors);
		}
	}

	@Override
	public UpdatePropertyResponse updateProperty(Property propertyToUpdate) {
		
		Property existingProperty = propertyRepository.findByPropertyId(propertyToUpdate.getId());
		
		List<ErrorCodes> errors = new ArrayList<ErrorCodes>();
		
		if (existingProperty != null) {
			
			existingProperty.update(propertyToUpdate);
			
			if (existingProperty.getPropertyName().length() == 0) {
				
				errors.add(ErrorCodes.PROPERTY_NAME_REQUIRED);
			}
			
			if (existingProperty.getMonthlyRent().length() == 0) {
				
				errors.add(ErrorCodes.PROPERTY_MONTHLY_RENT_REQUIRED);
			}
			
			if (existingProperty.getContactInfo().length() == 0) {
				
				errors.add(ErrorCodes.CONTACT_INFO_REQUIRED);
			}
			
			if (errors.size() == 0) {

				propertyRepository.updateProperty(existingProperty);
				
				return new UpdatePropertyResponse();
				
			} else {
				
				return new UpdatePropertyResponse(errors);
			}
			
		} else {
			
			errors.add(ErrorCodes.PROPERTY_NOT_FOUND);
			
			return new UpdatePropertyResponse(errors);
		}
	}
	
	@Override
	public PropertyListResponse findByAdminFilterData(String propertyName,
		String tenantName, String balance) {

		PropertyListResponse response = null;
		
		if (propertyRepository.isEmpty()) {
			
			List<ErrorCodes> errors = new ArrayList<ErrorCodes>();
			
			errors.add(ErrorCodes.PROPERTY_REPOSITORY_EMPTY);
			
			response = new PropertyListResponse(errors);
			
		} else {
			
			List<Property> propertyList 
				= propertyRepository.findByAdminFilterData(propertyName, tenantName, balance);
		
			response = new PropertyListResponse();
		
			response.setPropertyList(propertyList);
		}
			
		return response;
	}
	
	@Override
	public PropertyListResponse findByPublicFilterData(String address, String city, 
		String state, String zip, String monthlyRent) {

		List<Property> propertyList 
			= propertyRepository.findByPublicFilterData(address, city, state, zip, monthlyRent);
		
		PropertyListResponse response = new PropertyListResponse();
		
		response.setPropertyList(propertyList);
		
		return response;
	}

	@Override
	public PropertyDetailResponse getPropertyDetail(String propertyId) {
		
		Property property = propertyRepository.findByPropertyId(propertyId);
		
		if (property != null) {
			
			return new PropertyDetailResponse(property);
			
		} else {
			
			List<ErrorCodes> errors = new ArrayList<ErrorCodes>();
			
			errors.add(ErrorCodes.PROPERTY_NOT_FOUND);
			
			return new PropertyDetailResponse(errors);
		}
	}
}
